package com.feibi.study.Dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.feibi.study.R;


public class MyDialog {
    private Context context;
    private Dialog dialog;
    private FrameLayout rootLayout;
    private TextView tv_title;
    private TextView tv_msg;
    private ImageView iv_icon;
    private TextView tv_btn_left, tv_btn_right;
    private Display display;
    private boolean showFl = false;
    private boolean showIcon = false;
    private boolean showTitle = false;
    private boolean showMsg = false;
    private boolean showMsg2 = false;
    private boolean showLeftBtn = false;
    private boolean showRightBtn = false;

    public MyDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        builder();
    }

    private MyDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal, null);
        initViews(view);
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
        rootLayout.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.72), ViewGroup.LayoutParams.WRAP_CONTENT));
        setGone();
        return this;
    }

    private void initViews(View view) {
        iv_icon = view.findViewById(R.id.iv_icon);
        tv_title = view.findViewById(R.id.tv_title);
        tv_msg = view.findViewById(R.id.tv_msg);
        tv_btn_left = view.findViewById(R.id.tv_btn_left);
        tv_btn_right = view.findViewById(R.id.tv_btn_right);
        rootLayout = view.findViewById(R.id.rootLayout);
    }

    /**
     * 恢复初始
     *
     */
    private MyDialog setGone() {
        showTitle = false;
        showMsg = false;
        showLeftBtn = false;
        showRightBtn = false;
        showMsg2 = false;
        showIcon = false;
        showFl = false;
        if (rootLayout != null) {
            setLayout();
        }
        return this;
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public MyDialog setIcon(int res) {
        showIcon = true;
        iv_icon.setImageDrawable(context.getDrawable(res));
        return this;
    }


    /**
     * 设置title
     *
     * @param title
     * @return
     */
    public MyDialog setTitle(String title, int color) {
        showTitle = true;
        tv_title.setTextColor(color);
        if (TextUtils.isEmpty(title)) {
            tv_title.setText("Title");
        } else {
            tv_title.setText(title);
        }
        return this;
    }

    public MyDialog setTitle(String title) {
        showTitle = true;
        tv_title.setTextColor(context.getResources().getColor(R.color.black));
        if (TextUtils.isEmpty(title)) {
            tv_title.setText("Title");
        } else {
            tv_title.setText(title);
        }
        return this;
    }

    /**
     * 设置Message
     *
     * @param msg
     * @return
     */
    public MyDialog setMsg(String msg) {
        showMsg = true;
        if (TextUtils.isEmpty(msg)) {
            tv_msg.setText("");
        } else {
            tv_msg.setText(msg);
        }
        return this;
    }


    /**
     * 设置点击外部是否消失
     *
     * @param cancel
     * @return
     */
    public MyDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    /**
     * 左侧按钮
     *
     * @param text
     * @param listener
     * @return
     */
    public MyDialog setLeftBtn(String text, final View.OnClickListener listener) {
        return setLeftBtn(text, -1, listener);
    }

    public MyDialog setLeftBtn(String text, int color, final View.OnClickListener listener) {
        //tip:後期如有需要，可自定義字體顏色與背景
        showLeftBtn = true;
        if (text == null) {
            tv_btn_left.setText("");
        } else {
            tv_btn_left.setText(text);
        }
        tv_btn_left.setOnClickListener(v -> {
            if (listener != null)
                listener.onClick(v);
            dismiss();
        });
        return this;
    }

    /**
     * 右侧按钮
     *
     * @param text
     * @param listener
     * @return
     */
    public MyDialog setRightBtn(String text, final View.OnClickListener listener) {
        return setRightBtn(text, -1, listener);
    }

    public MyDialog setRightBtn(String text, int color, final View.OnClickListener listener) {
        //tip:後期如有需要，可自定義字體顏色與背景
        showRightBtn = true;
        if (text == null) {
            tv_btn_right.setText("");
        } else {
            tv_btn_right.setText(text);
        }
        tv_btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(v);
                dismiss();
            }
        });
        return this;
    }

    /**
     * 设置显示
     */
    private void setLayout() {
        iv_icon.setVisibility(showIcon ? View.VISIBLE : View.GONE);
        tv_title.setVisibility(showTitle ? View.VISIBLE : View.GONE);
        tv_msg.setVisibility(showMsg ? View.VISIBLE : View.GONE);
        tv_btn_left.setVisibility(showLeftBtn ? View.VISIBLE : View.GONE);
        tv_btn_right.setVisibility(showRightBtn ? View.VISIBLE : View.GONE);
    }

    public void show() {
        setLayout();
        dialog.show();
    }

    public boolean isShowing() {
        if (dialog != null) {
            if (dialog.isShowing())
                return true;
            else
                return false;
        }
        return false;
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}


